package com.jjb.www.ui.adapter;

import static com.umeng.socialize.utils.ContextUtil.getContext;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.http.api.CurriculumApi;
import com.jjb.www.http.api.CurriculumDetitleApi;
import com.jjb.www.http.glide.GlideApp;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author CRG
 * @date :2022/8/4 9:37
 * @description:
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<CurriculumApi.Bean.ListDTO> zhangList;
    private OnListener mOnListener;

    private int mGroupdex = -1;
    private int mChilddex = -1;

    public ExpandableListViewAdapter(Context context, List<CurriculumApi.Bean.ListDTO> mzhangList, OnListener onListener) {
        this.context = context;
        this.mOnListener = onListener;
        this.zhangList = mzhangList;
    }

    //    public void setData(CurriculumApi.Bean bean) {
//        zhangList = bean;
//
//        notifyDataSetChanged();
//    }
    public void setItemSel(int groupdex, int childdex) {
        this.mGroupdex = groupdex;
        this.mChilddex = childdex;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return zhangList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return zhangList == null || zhangList.size() == 0 ? 0 : zhangList.get(groupPosition).getChapter_list().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return zhangList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return zhangList.get(groupPosition).getChapter_list().get(childPosition);

    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chapter_title, parent, false);
        }
        holder = new GroupViewHolder(convertView);
//每个组对象
        CurriculumApi.Bean.ListDTO chapter = zhangList.get(groupPosition);
        if (chapter.getChapter_list() == null || chapter.getChapter_list().size() == 0) {
            holder.mTvType2.setVisibility(View.VISIBLE);
        }
            holder.mTvType.setVisibility(View.VISIBLE);
            if (chapter.isIsfree()) {
                //1专业课程 2公共课程
                if (chapter.getType() == 1) {
                    holder.mTvType.setText("专业");
                    holder.mTvType.getShapeDrawableBuilder()
                            .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_EAF1FE))
                            .intoBackground();
                    holder.mTvType.getTextColorBuilder()
                            .setTextColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                            .intoTextColor();
                } else {
                    holder.mTvType.setText("公共");
                    holder.mTvType.getShapeDrawableBuilder()
                            .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_E5F9EF))
                            .intoBackground();
                    holder.mTvType.getTextColorBuilder()
                            .setTextColor(ContextCompat.getColor(getContext(), R.color.color_09BB07))
                            .intoTextColor();
                }
            } else {
                //0选修 1必修
                if (chapter.getIs_must() == 0) {
                    holder.mTvType.setText("选修");
                    holder.mTvType.getShapeDrawableBuilder()
                            .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_E5F9EF))
                            .intoBackground();
                    holder.mTvType.getTextColorBuilder()
                            .setTextColor(ContextCompat.getColor(getContext(), R.color.color_09BB07))
                            .intoTextColor();

                } else {
                    holder.mTvType.setText("必修");
                    holder.mTvType.getShapeDrawableBuilder()
                            .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_EAF1FE))
                            .intoBackground();
                    holder.mTvType.getTextColorBuilder()
                            .setTextColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                            .intoTextColor();
                }
            }

        if (groupPosition == mGroupdex) {
            holder.tvCourseChapter.setTextColor(convertView.getResources().getColor(R.color.color_3675FE));
        } else {
            holder.tvCourseChapter.setTextColor(convertView.getResources().getColor(R.color.color_151515));
        }
        holder.tvCourseChapter.setText(chapter.getNumber() + " " + chapter.getTitle());
        if (isExpanded) {
            holder.ivRetractSpread.setImageResource(R.mipmap.icon_chapter_up);
        } else {
            holder.ivRetractSpread.setImageResource(R.mipmap.icon_chapter_down);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chapter_contents, parent, false);
        }
        holder = new ItemViewHolder(convertView);
        //每个节对象
        final CurriculumApi.Bean.ListDTO.ChapterListDTO section =
                zhangList.get(groupPosition).getChapter_list().get(childPosition);
        holder.tvContentTitle.setText(section.getNumber() + section.getChapter_title());
        holder.tvContentTime.setText("时长 " + section.getDuration());
        //0小节未学习 1小节正在学 2小节已学完
        if (section.getState() != null) {
            if (section.getState() == 0) {
                holder.mTvXxType.setText("未学习");
            } else if (section.getState() == 1) {
                holder.mTvXxType.setText("已学" + section.getProgress() + "%");
            } else if (section.getState() == 2) {
                holder.mTvXxType.setText("已学完");
            }
        } else {
            holder.mTvXxType.setText("未学习");
        }


        if (groupPosition == mGroupdex && childPosition == mChilddex) {
            holder.tvContentTitle.setTextColor(convertView.getResources().getColor(R.color.color_3675FE));
            holder.tvContentTime.setTextColor(convertView.getResources().getColor(R.color.color_3675FE));
            holder.mTvXxType.setTextColor(convertView.getResources().getColor(R.color.color_3675FE));
            holder.mImgGif.setVisibility(View.VISIBLE);
            GlideApp.with(convertView.getContext())
                    .load(R.drawable.ic_vide)
                    .into(holder.mImgGif);
        } else {
            holder.mImgGif.setVisibility(View.INVISIBLE);
            holder.tvContentTitle.setTextColor(convertView.getResources().getColor(R.color.color_151515));
            holder.tvContentTime.setTextColor(convertView.getResources().getColor(R.color.color_151515));
            holder.mTvXxType.setTextColor(convertView.getResources().getColor(R.color.color_151515));
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 父项的view搭载器
     */
    private static class GroupViewHolder {
        private final AppCompatTextView tvCourseChapter;
        private final AppCompatImageView ivRetractSpread;
        private final ShapeTextView mView;
        private ShapeTextView mTvType;
        private ShapeTextView mTvType2;




        private GroupViewHolder(View itemView) {
            mTvType2 = itemView.findViewById(R.id.tv_type2);
            mTvType = itemView.findViewById(R.id.tv_type);
            tvCourseChapter = itemView.findViewById(R.id.tv_title);
            ivRetractSpread = itemView.findViewById(R.id.iv_retract_spread);
            mView = itemView.findViewById(R.id.view_dotted_line);

        }
    }

    /**
     * 子项的搭载器
     */
    private static class ItemViewHolder {
        private final TextView tvContentTitle;
        private final TextView tvContentTime;
        private final ShapeTextView mTvXxType;
        private final ShapeImageView mImgGif;


        private ItemViewHolder(View itemView) {
            mTvXxType = itemView.findViewById(R.id.tv_xx_type);
            tvContentTitle = itemView.findViewById(R.id.tv_content_title);
            tvContentTime = itemView.findViewById(R.id.tv_content_time);
            mImgGif = itemView.findViewById(R.id.img_gif);
        }
    }

    public interface OnListener {

        /**
         * 选择条目时回调
         */
        void onSelected(int position);
    }
}
